﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace AndreasJohansson.AutoMailic
{
  /// <summary>
  /// Represents one configuration section in the configuration file.
  /// </summary>
  class ConfigurationSection
  {
    /// <summary>
    /// Constructor
    /// </summary>
    /// <param name="sectionNode">XmlNode for the section this object represents</param>
    /// <exception cref="System.ArgumentNullException">Thrown when no valid sectionNode is supplied</exception>
    public ConfigurationSection(XmlNode sectionNode)
    {
      if (null == sectionNode)
      {
        throw new ArgumentNullException("sectionNode", "Must have valid XmlNode");
      }

      _sectionNode = sectionNode;
    }

    /// <summary>Get the configuration sections name (@name)</summary>
    public string Name
    {
      get
      {
        return GetNodeValueAsString("@name");
      }
    }

    /// <summary>Get connection type</summary>
    public string ConnectionType
    {
      get
      {
        return GetNodeValueAsString("database/@connectionType");
      }
    }

    /// <summary>Get connection string</summary>
    public string ConnectionString
    {
      get
      {
        return GetNodeValueAsString("database/@connectionString");
      }
    }

    public string TokenDelimiter
    {
      get
      {
        return GetNodeValueAsString("database/@tokenDelimiter");
      }
    }

    /// <summary>Get sql statement to get all items to process</summary>
    public string SqlItems
    {
      get
      {
        return GetNodeValueAsString("sql/items/text()");
      }
    }

    /// <summary>Get sql statement to execute when item processed successfully</summary>
    public string SqlOnSuccess
    {
      get
      {
        return GetNodeValueAsString("sql/onSuccess/text()");
      }
    }

    /// <summary>Get sql statement to execute if error occured while processing</summary>
    public string SqlOnError
    {
      get
      {
        return GetNodeValueAsString("sql/onError/text()");
      }
    }

    /// <summary>Get smtp server to use</summary>
    public string SmtpServer
    {
      get
      {
        return GetNodeValueAsString("smtp/@server");
      }
    }

    /// <summary>Get smtp port to use</summary>
    public int? SmtpPort
    {
      get
      {
        return GetNodeValueAsInt32("smtp/@port");
      }
    }

    /// <summary>Type of mail HTML/text</summary>
    public string MailType
    {
      get
      {
        return GetNodeValueAsString("mail/@type");
      }
    }

    /// <summary>From address, may use tokens to get data from record</summary>
    public string MailFrom
    {
      get
      {
        return GetNodeValueAsString("mail/@from");
      }
    }

    /// <summary>To address, may use tokens to get data from record</summary>
    public string MailTo
    {
      get
      {
        return GetNodeValueAsString("mail/@to");
      }
    }

    /// <summary>Subject, may use tokens to get data from record</summary>
    public string MailSubject
    {
      get
      {
        return GetNodeValueAsString("mail/@subject");
      }
    }

    /// <summary>Mail template, may use tokens to get data from record</summary>
    public string MailTemplate
    {
      get
      {
        return GetNodeValueAsString("mail/text()");
      }
    }

    /// <summary>
    /// Gets the nodes value as string for
    /// the xpath using the section node
    /// </summary>
    /// <param name="xpath">xpath expression to node</param>
    /// <returns>String with value of node or null if failure</returns>
    private string GetNodeValueAsString(string xpath)
    {
      if (null == _sectionNode)
      {
        return null;
      }

      XmlNode node = _sectionNode.SelectSingleNode(xpath);
      return (null == node) ? null : node.Value;
    }

    /// <summary>
    /// Gets the nodes value as int for
    /// the xpath using the section node
    /// </summary>
    /// <param name="xpath">xpath expression to node</param>
    /// <returns>Int32 with value of node or null if failure</returns>
    private Int32? GetNodeValueAsInt32(string xpath)
    {
      if (null == _sectionNode)
      {
        return null;
      }

      XmlNode node = _sectionNode.SelectSingleNode(xpath);
      if (null == node)
      {
        return null;
      }
      Int32 value;
      if (false == Int32.TryParse(node.Value, out value))
      {
        return null;
      }

      return value;
    }

    /// <summary>The section node</summary>
    private XmlNode _sectionNode = null;
  }
}
